//导入数据库
const db=require('../db/index')
//导入bcryptjs
const bcrypt=require('bcryptjs')
const moment=require('moment')

exports.operatorall=(req,res)=>{
    const sql='select * from operator'
    db.query(sql,(err,results)=>{
        if(err) return res.cc(err)
        res.json(results)
    })
}

exports.find=(req,res)=>{
    var info=req.body
    var type=info.selecttype
    var sql=` select * from operator `
    if(info.inputtext.length!=0){
        var name=info.inputtext
        sql+=` where `
        if(type=='oper_name')
        {
            sql+=` oper_name like '%${name}%' `
        }else if(type=='oper_email')
        {
            sql+=` oper_email = '${name}' `
        }else if(type=='oper_tel')
        {
            sql+=` oper_tel = '${name}' `
        }
    }
    
    if(info.start_date.length!=0||info.end_date.length!=0)
    {
           
    var start_date=moment(info.start_date).format('YYYY-MM-DD')
    var end_date=moment(info.end_date).format('YYYY-MM-DD')
        if(info.inputtext.length!=0){
            sql+=` and `
        }else if(info.inputtext.length==0)
        {
            sql+=` where `
        }
        if(info.start_date.length!=0&&info.end_date.length!=0){
            sql+=` oper_hiredate >= '${start_date}' and oper_hiredate <= '${end_date}' `
        }else if(info.start_date.length!=0){
            sql+=` oper_hiredate >= '${start_date}' `
        }else if(info.end_date.length!=0){
            sql+=` oper_hiredate <= '${end_date}' `
        }
    }
    console.log(sql)
    db.query(sql,(err,results)=>{
        if(err) return res.cc(err)
        res.json(results)
    })
}

exports.operatordelete=(req,res)=>{
    const operatorinfo=req.body
    const sql='delete from operator where oper_id=?'
    db.query(sql,operatorinfo.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1) return res.cc('failed to delete')
        res.cc('delete successfully',0)
    })
}
exports.operatorupdate=(req,res)=>{
    const operatorinfo=req.body
    const date=moment(operatorinfo.hiredate).format('YYYY-MM-DD')
    const sql='update operator set ? where oper_id=?'
    db.query(sql,[{oper_name:operatorinfo.username,oper_email:operatorinfo.email,oper_tel:operatorinfo.tel,oper_hiredate:date},operatorinfo.id],(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to update')
        res.cc('update successful',0)
    })
}

exports.operatorreset=(req,res)=>{
    const operatorinfo=req.body
    operatorinfo.password=bcrypt.hashSync(operatorinfo.password,10)
    const sql='update operator set ? where oper_id=?'
    db.query(sql,[{oper_password:operatorinfo.password},operatorinfo.id],(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to reset')
        res.cc('reset successful',0)
    })
}

exports.operatorinsert=(req,res)=>{
    const operatorinfo=req.body
    const date=moment(operatorinfo.hiredate).format('YYYY-MM-DD')
    operatorinfo.password=bcrypt.hashSync(operatorinfo.password,10)
    const sql='select * from operator where oper_email=?'
    db.query(sql,operatorinfo.username,(err,results)=>{
        if(err)return res.cc(err)
        if(results.length==1) res.cc('email address is exist,please change and try again')
        const sql='insert into operator set ? '
        db.query(sql,{oper_name:operatorinfo.username,oper_password:operatorinfo.password,oper_email:operatorinfo.email,oper_tel:operatorinfo.tel,oper_hiredate:date},(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows!=1)return res.cc('failed to insert')
        res.cc('insert successful',0)
    })
    })  
}

